package com.cincc.cinccimdemo.ui.conversation.detail;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.cincc.cinccimdemo.R;
import com.cincc.cinccimdemo.adapter.ConversationDetailMemberAdapter;
import com.cincc.cinccimdemo.common.AttrConstants;
import com.cincc.cinccimdemo.common.MyUtils;
import com.cincc.cinccimdemo.model.ConversationInfo;
import com.cincc.cinccimdemo.ui.IMBaseActivity;
import com.cincc.cinccimdemo.ui.conversation.member.AddMemberActivity;
import com.cincc.cinccimdemo.widget.OptionItemView;
import com.cincc.im.callback.ImCallback;
import com.cincc.im.core.IMSipPhoneCtrl;
import com.cincc.im.model.ImResponse;
import com.cincc.im.model.dbmodel.FriendInfo;
import com.cincc.im.model.dbmodel.GroupMemberInfo;
import com.cincc.im.model.enums.DisturbState;
import com.google.android.material.switchmaterial.SwitchMaterial;

import java.util.ArrayList;
import java.util.List;

public class SingleDetailActivity extends IMBaseActivity implements CompoundButton.OnCheckedChangeListener {
    private static final String TAG = "SingleDetailActivity";

    @Override
    protected int contentLayout() {
        return R.layout.activity_single_conversation_detail;
    }

    RecyclerView recyclerView;
    SwitchMaterial silentSwitchButton;
    SwitchMaterial stickTopSwitchButton;
    OptionItemView clearMessagesOptionItemView;

    @Override
    protected void bindViews() {
        super.bindViews();
        recyclerView = findViewById(R.id.memberRecyclerView);
        silentSwitchButton = findViewById(R.id.silentSwitchButton);
        stickTopSwitchButton = findViewById(R.id.stickTopSwitchButton);

        clearMessagesOptionItemView = findViewById(R.id.clearMessagesOptionItemView);
    }

    @Override
    protected void bindEvents() {
        super.bindEvents();
        silentSwitchButton.setOnCheckedChangeListener(this);
        stickTopSwitchButton.setOnCheckedChangeListener(this);
        clearMessagesOptionItemView.setOnClickListener(v -> clearHistory());
    }

    ConversationInfo conversationInfo;

    @Override
    protected void afterViews() {
        super.afterViews();
        conversationInfo = getIntent().getParcelableExtra("conversationInfo");
        boolean top = IMSipPhoneCtrl.getInstance().isTop(conversationInfo.imSendType, conversationInfo.targetId);
        boolean noDisturb = IMSipPhoneCtrl.getInstance().isNoDisturb(conversationInfo.imSendType, conversationInfo.targetId);
        silentSwitchButton.setChecked(noDisturb);
        stickTopSwitchButton.setChecked(top);

        List<GroupMemberInfo> memberInfos = new ArrayList<>();
        FriendInfo friendInfo = IMSipPhoneCtrl.getInstance().getFriendInfo(conversationInfo.targetId);
        GroupMemberInfo memberInfo = new GroupMemberInfo();
        memberInfo.nickName = TextUtils.isEmpty(friendInfo.alias) ? friendInfo.nickName : friendInfo.alias;
        memberInfo.protraitUrl = friendInfo.protraitUrl;
        memberInfos.add(memberInfo);
        ConversationDetailMemberAdapter memberAdapter = new ConversationDetailMemberAdapter(this, true, false);
        recyclerView.setAdapter(memberAdapter);
        memberAdapter.setList(memberInfos);
        memberAdapter.setOnMemberClickListener(new ConversationDetailMemberAdapter.OnMemberClickListener() {
            @Override
            public void addMemberClick() {
                addMember();
            }

            @Override
            public void delMemberClick() {

            }
        });
    }

    private void addMember() {
        String[] checkedUids = new String[]{conversationInfo.targetId};
        Intent i = new Intent(this, AddMemberActivity.class);
        i.putExtra(AttrConstants.MEMBER_IDS, checkedUids);
        startActivity(i);
    }

    private void setConversationTop(boolean isCheck) {
        IMSipPhoneCtrl.getInstance().setTop(conversationInfo.imSendType, conversationInfo.targetId, isCheck);
        MyUtils.fireConversationListRefreshEvent();
    }

    private void setConversationNoDisturb(boolean isCheck) {
        IMSipPhoneCtrl.getInstance().disturb(conversationInfo.imSendType, isCheck ? DisturbState.ON : DisturbState.OFF, conversationInfo.targetId, new ImCallback() {
            @Override
            public void callback(ImResponse imResponse) {
                Log.e(TAG, "setConversationNoDisturb callback: " + imResponse.getCode());
                String result = imResponse.getCode() == 0 ? (isCheck ? "设置免打扰成功" : "取消免打扰成功") : imResponse.getMsg();
                runOnUiThread(() -> {
                    Toast.makeText(SingleDetailActivity.this, result, Toast.LENGTH_SHORT).show();
                    MyUtils.fireConversationListRefreshEvent();
                });
            }
        });
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        int id = buttonView.getId();
        if (id == R.id.stickTopSwitchButton) {
            setConversationTop(isChecked);
        } else if (id == R.id.silentSwitchButton) {
            setConversationNoDisturb(isChecked);
        }
    }

    private void clearHistory() {
        IMSipPhoneCtrl.getInstance().clearHistoryMsg(conversationInfo.imSendType, conversationInfo.targetId, new ImCallback() {
            @Override
            public void callback(ImResponse imResponse) {
                Log.e(TAG, "clearHistory callback: " + imResponse.getCode());
                String result = imResponse.getCode() == 0 ? "清空聊天记录成功" : imResponse.getMsg();
                runOnUiThread(() -> {
                    Toast.makeText(SingleDetailActivity.this, result, Toast.LENGTH_SHORT).show();
                    MyUtils.fireConversationRefreshEvent();
                    MyUtils.fireConversationListRefreshEvent();
                });
            }
        });
    }
}
